<template>
  <view class="uni-container">
    <uni-forms ref="form" :model="formData" validateTrigger="bind">
      <uni-forms-item name="order_id" label="订单ID" required>
        <uni-easyinput placeholder="关联的订单ID" v-model="formData.order_id"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="merchant_id" label="商户ID" required>
        <uni-easyinput placeholder="被评价的商户ID" v-model="formData.merchant_id"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="user_id" label="用户ID" required>
        <uni-easyinput placeholder="评价用户的ID" v-model="formData.user_id"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="logistics_rating" label="物流服务评分" required>
        <picker @change="pickerChange($event, 'logistics_rating')" :range="formOptions.logistics_rating_data" :value="formOptions.logistics_rating_index">
          <view>{{formOptions.logistics_rating_data[formOptions.logistics_rating_index] || "请选择..."}}</view>
        </picker>
      </uni-forms-item>
      <uni-forms-item name="service_rating" label="服务态度评分" required>
        <picker @change="pickerChange($event, 'service_rating')" :range="formOptions.service_rating_data" :value="formOptions.service_rating_index">
          <view>{{formOptions.service_rating_data[formOptions.service_rating_index] || "请选择..."}}</view>
        </picker>
      </uni-forms-item>
      <uni-forms-item name="overall_rating" label="综合评分">
        <undefined v-model="formData.overall_rating"></undefined>
      </uni-forms-item>
      <uni-forms-item name="review_content" label="评价内容">
        <uni-easyinput placeholder="对商户的文字评价" v-model="formData.review_content"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="is_anonymous" label="是否匿名">
        <switch @change="binddata('is_anonymous', $event.detail.value)" :checked="formData.is_anonymous"></switch>
      </uni-forms-item>
      <uni-forms-item name="status" label="评价状态">
        <uni-data-checkbox v-model="formData.status" :localdata="formOptions.status_localdata"></uni-data-checkbox>
      </uni-forms-item>
      <uni-forms-item name="reply_info" label="商家回复">
        <undefined v-model="formData.reply_info"></undefined>
      </uni-forms-item>
      <uni-forms-item name="create_time" label="创建时间">
        <uni-datetime-picker return-type="timestamp" v-model="formData.create_time"></uni-datetime-picker>
      </uni-forms-item>
      <view class="uni-button-group">
        <button type="primary" class="uni-button" style="width: 100px;" @click="submit">提交</button>
        <navigator open-type="navigateBack" style="margin-left: 15px;">
          <button class="uni-button" style="width: 100px;">返回</button>
        </navigator>
      </view>
    </uni-forms>
  </view>
</template>

<script>
  import { validator } from '../../js_sdk/validator/intangible_merchantreviews.js';

  const db = uniCloud.database();
  const dbCmd = db.command;
  const dbCollectionName = 'intangible_merchantreviews';

  function getValidator(fields) {
    let result = {}
    for (let key in validator) {
      if (fields.includes(key)) {
        result[key] = validator[key]
      }
    }
    return result
  }

  function numberRange(start, end) {
    return Array.from(new Array(end + 1).keys()).slice(start);
  }

  export default {
    data() {
      let formData = {
        "order_id": "",
        "merchant_id": "",
        "user_id": "",
        "logistics_rating": 1,
        "service_rating": 1,
        "overall_rating": null,
        "review_content": "",
        "is_anonymous": false,
        "status": 0,
        "reply_info": null,
        "create_time": null
      }
      return {
        formData,
        formOptions: {
          "logistics_rating_data": numberRange(1, 5),
          "logistics_rating_index": null,
          "service_rating_data": numberRange(1, 5),
          "service_rating_index": null,
          "status_localdata": [
            {
              "value": 0,
              "text": 0
            },
            {
              "value": 1,
              "text": 1
            },
            {
              "value": 2,
              "text": 2
            }
          ]
        },
        rules: {
          ...getValidator(Object.keys(formData))
        }
      }
    },
    onReady() {
      this.$refs.form.setRules(this.rules)
    },
    methods: {
      pickerChange(e, name) {
        this.formOptions[name + '_index'] = e.detail.value;
        this.formData[name] = this.formOptions[name + '_data'][e.detail.value];
        this.binddata(name, this.formData[name]);
      },
      /**
       * 验证表单并提交
       */
      submit() {
        uni.showLoading({
          mask: true
        })
        this.$refs.form.validate().then((res) => {
          return this.submitForm(res)
        }).catch(() => {
        }).finally(() => {
          uni.hideLoading()
        })
      },

      /**
       * 提交表单
       */
      submitForm(value) {
        // 使用 clientDB 提交数据
        return db.collection(dbCollectionName).add(value).then((res) => {
          uni.showToast({
            title: '新增成功'
          })
          this.getOpenerEventChannel().emit('refreshData')
          setTimeout(() => uni.navigateBack(), 500)
        }).catch((err) => {
          uni.showModal({
            content: err.message || '请求服务失败',
            showCancel: false
          })
        })
      }
    }
  }
</script>
